package com.changpay.zywy.wight;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.*;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import com.changpay.zywy.R;

public class ColumnarView extends View {
    private final String TAG = getClass().getSimpleName();
    private int mWidth = dp2Px(50), mHeight = dp2Px(300), mCenterX, mCenterY;

    private Paint mDefaultPaint;
    private float present = 0.5f;
    private Paint p;
    private Path path1;
    private Path path2;
    private int topColor;
    private int topLineOneColor;
    private int topLineTwoColor;
    private int bottomColor;
    private int bottomLineOneColor;
    private int bottomLineTwoColor;

    public ColumnarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        // 获取自定义属性值
        getAttrs(context, attrs);
        // 初始化
        init();
    }


    /**
     * 用于获取Xml文件中的自定义属性
     */
    private void getAttrs(Context context, AttributeSet attrs) {

                TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleZhuView);
                topColor = a.getColor(R.styleable.CircleZhuView_top_color, Color.parseColor("#FE6058"));
                topLineOneColor = a.getColor(R.styleable.CircleZhuView_top_line_color_one,Color.parseColor("#d71e14"));
                topLineTwoColor = a.getColor(R.styleable.CircleZhuView_top_line_color_two, Color.parseColor("#f8756f"));
                bottomColor = a.getColor(R.styleable.CircleZhuView_bottom_color, Color.parseColor("#735668"));
                bottomLineOneColor = a.getColor(R.styleable.CircleZhuView_bottom_line_color_one, Color.parseColor("#4e4049"));
                bottomLineTwoColor = a.getColor(R.styleable.CircleZhuView_bottom_line_color_two,Color.parseColor("#735668"));
                a.recycle();//不要忘记回收！
    }

    /**
     * 用于初始化动画，画笔和路径
     */

    private void init() {
        mDefaultPaint = new Paint(Paint.ANTI_ALIAS_FLAG);  //抗锯齿标志
        mDefaultPaint.setStyle(Paint.Style.FILL);  // Paint.Style.FILL：填充内部  Paint.Style.FILL_AND_STROKE ：填充内部和描边  Paint.Style.STROKE ：描边
        p = new Paint(Paint.ANTI_ALIAS_FLAG);
        p.setStyle(Paint.Style.FILL);
        path1 = new Path();
        path2 = new Path();

    }


    /**
     * 在控件大小发生改变时调用。所以这里初始化会被调用一次
     */
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
        mCenterX = w / 2;
        mCenterY = h / 2;
    }


    /**
     * 测量View的大小，也可以通过下面方式，修改View的大小
     */

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = getMeasureSize(true, widthMeasureSpec);
        int height = getMeasureSize(false, heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    /**
     * 获取View尺寸
     * 基本上算是标准写法
     *
     * @param isWidth 是否是width，不是的话，是height
     */
    private int getMeasureSize(boolean isWidth, int measureSpec) {

        int result = 0;

        int specSize = MeasureSpec.getSize(measureSpec);
        int specMode = MeasureSpec.getMode(measureSpec);

        switch (specMode) {
            case MeasureSpec.UNSPECIFIED:
                if (isWidth) {
                    result = getSuggestedMinimumWidth();
                } else {
                    result = getSuggestedMinimumHeight();
                }
                break;
            case MeasureSpec.AT_MOST:   // at_most中的specsize为子view可以拿到的最大值
                if (isWidth)
                    result = Math.min(specSize, mWidth);
                else
                    result = Math.min(specSize, mHeight);
                break;
            case MeasureSpec.EXACTLY:
                result = specSize;
                break;
        }
        return result;
    }


    /**
     * 绘制图形了
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        path1.reset();
        path2.reset();
        // 坐标原点移动到View 中心
        canvas.translate(mCenterX, mCenterY);

        // 上半部分圆柱

        mDefaultPaint.setColor(bottomColor);
        RectF rectF1 = new RectF(-mCenterX, mCenterX / 3 , mCenterX, mCenterX / 3 + mCenterY/4 * present);
        RectF oval0 = new RectF(-mCenterX, -mCenterX / 6 + mCenterY/4 * present, mCenterX, mCenterX *5/6 + mCenterY/4 * present);
        path1.addRect(rectF1, Path.Direction.CW);
        path1.addOval(oval0, Path.Direction.CW);
        canvas.drawPath(path1,mDefaultPaint);

        LinearGradient lg0 = new LinearGradient(-mCenterX, mCenterX*5/6, -mCenterX, -mCenterX / 6, bottomLineOneColor
                , bottomLineTwoColor, Shader.TileMode.REPEAT);
        p.setShader(lg0);
        RectF oval1 = new RectF(-mCenterX, mCenterX*5/6, mCenterX, -mCenterX/6);
        canvas.drawOval(oval1, p);

        // 下半部分圆柱

        RectF oval2 = new RectF(-mCenterX, mCenterX / 2, mCenterX, -mCenterX / 2);
        RectF rectF2 = new RectF(-mCenterX, 0, mCenterX, -mCenterY / 2*present);// 设置个新的长方形，扫描测量
        RectF oval3 = new RectF(-mCenterX, mCenterX / 2 - mCenterY / 2*present, mCenterX, (-mCenterX / 2) - mCenterY / 2*present);// 设置个新的长方形，扫描测量
        path2.addOval(oval2, Path.Direction.CW);
        path2.addRect(rectF2, Path.Direction.CW);

        mDefaultPaint.setColor(topColor);
        canvas.drawPath(path2, mDefaultPaint);
        LinearGradient lg = new LinearGradient(-mCenterX, mCenterX / 2 - mCenterY / 2*present, -mCenterX, (-mCenterX / 2) - mCenterY / 2*present, topLineOneColor
                ,topLineTwoColor, Shader.TileMode.REPEAT);
        p.setShader(lg);
        canvas.drawOval(oval3, p);


    }


    private int dp2Px(int dp) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    public void setPresent(float present)
    {
        this.present = present;
        this.invalidate();
    }
}






